<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

/**
 * 组逻辑
 */
class Group extends AdminBase
{

    /**
     * 获取组列表
     */
    public function getGroupList($where = [], $field = 'a.*,b.name,c.branch_name', $order = 'a.sort desc', $paginate=0)
    {
        $this->modelGroup->alias('a');
        
        $join = [
                    [SYS_DB_PREFIX . 'company b', 'a.company_id = b.id'],
                    [SYS_DB_PREFIX . 'branch c', 'a.branch_id = c.id'],
                ];
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        return $this->modelGroup->getList($where, $field, $order, $paginate,$join);
    }
    
    /**
     * 获取组列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['group_name']) && $where['a.group_name'] = ['like', '%'.$data['group_name'].'%'];
        !empty($data['branch_id']) && $where['a.branch_id'] = ['eq', $data['branch_id']];
        !empty($data['company_id']) && $where['a.company_id'] = ['eq', $data['company_id']];
        return $where;
    }

    /**
     * 导出组列表
     */
    public function exportGroupList($where = [], $field = 'a.*,b.name,c.branch_name', $order = '')
    {
        
        $list = $this->getGroupList($where, $field, $order, false);
        
        $titles = "组名,组负责人,所属部门,所属公司,注册时间";
        $keys   = "group_name,principal,branch_name,name,create_time";
        
        action_log('导出', '导出组列表');
        
        export_excel($titles, $keys, $list, '组列表');
    }
    
    /**
     * 组添加
     */
    public function groupAdd($data = [])
    {
        
        $validate_result = $this->validateGroup->scene('add')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateGroup->getError()];
        }
        
        $url = url('groupList');
        
        $result = $this->modelGroup->setInfo($data);
        
        $result && action_log('新增', '新增组，name：' . $data['group_name']);
        
        return $result ? [RESULT_SUCCESS, '组添加成功', $url] : [RESULT_ERROR, $this->modelGroup->getError()];
    }
    
    /**
     * 获取组信息
     */
    public function getGroupInfo($where = [])
    {
        
        return $this->modelgroup->getInfo($where);
    }

    /**
     * 组编辑
     */
    public function groupEdit($data = [])
    {
        
        $validate_result = $this->validateGroup->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateGroup->getError()];
        }
        $url = url('groupList');
        $result = $this->modelGroup->setInfo($data);
        
        $result && action_log('编辑', '编辑组，：name:' . $data['group_name']);
        
        return $result ? [RESULT_SUCCESS, '组编辑成功', $url] : [RESULT_ERROR, $this->modelGroup->getError()];
    }
    
    /**
     * 组删除
     */
    public function groupDel($where = [])
    {
        
        $url = url('groupList');
        
        if (SYS_ADMINISTRATOR_ID == $where['id'] || MEMBER_ID == $where['id']) {
            
            return [RESULT_ERROR, '天神和自己不能删除哦~', $url];
        }
        
        $result = $this->modelGroup->deleteInfo($where);
                
        $result && action_log('删除', '删除组，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '组删除成功', $url] : [RESULT_ERROR, $this->modelGroup->getError(), $url];
    }
}
